var express = require('express');
var router = express.Router();


router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});


router.get('/list', function(req, res, next) {

  var mongoose = require('mongoose');

  // 连接 test 数据库
  mongoose.connect('mongodb://localhost/test', {useMongoClient: true});

  // 连接成功
  mongoose.connection.on('connected', function () {    
    console.log('Mongoose connection open');  
  }); 

  // 配置 Schema & Model
  var Schema = mongoose.Schema;
  var colSchema = new Schema({
    title: String, 
    description: String,
    by: String,
    url: String,
    tags: [String],
    likes: Number
  }, {collection: 'col'});
  
  var Col = mongoose.model('col', colSchema);
  
  // 查找并返回结果
  Col.findOne( { likes: 100 }, 'title description', function(err, col){
    if (err) return handleError(err);
    console.log(col);
    res.render('listproduct', {'obj': col});
  });

});


module.exports = router;
